#include "PSFPhotonDetSD.hh"

#include "PSFPhotonDetHit.hh"
#include "PSFUserTrackInformation.hh"

#include "G4OpticalPhoton.hh"
#include "G4SDManager.hh"
#include "G4Step.hh"
#include "G4ThreeVector.hh"
#include "G4TouchableHistory.hh"
#include "G4Track.hh"
#include "G4VTouchable.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

PSFPhotonDetSD::PSFPhotonDetSD(G4String name)
    : G4VSensitiveDetector(name)
{
  collectionName.insert("PhotonDetHitCollection");
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void PSFPhotonDetSD::Initialize(G4HCofThisEvent *HCE)
{
  fPhotonDetHitCollection =
      new PSFPhotonDetHitsCollection(SensitiveDetectorName, collectionName[0]);

  if (fHCID < 0)
  {
    fHCID =
        G4SDManager::GetSDMpointer()->GetCollectionID(fPhotonDetHitCollection);
  }
  HCE->AddHitsCollection(fHCID, fPhotonDetHitCollection);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

G4bool PSFPhotonDetSD::ProcessHits(G4Step *aStep, G4TouchableHistory *)
{
  if (!aStep)
    return false;
  G4Track *theTrack = aStep->GetTrack();

  // Need to know if this is an optical photon
  if (theTrack->GetDefinition() != G4OpticalPhoton::OpticalPhotonDefinition())
  {
    return false;
  }

  // Find out information regarding the hit
  G4StepPoint *thePostPoint = aStep->GetPostStepPoint();

  auto trackInformation =
      (PSFUserTrackInformation *)theTrack->GetUserInformation();

  auto theTouchable = (G4TouchableHistory *)(thePostPoint->GetTouchable());

  G4ThreeVector photonExit = trackInformation->GetExitPosition();
  G4ThreeVector photonArrive = thePostPoint->GetPosition();
  G4double arrivalTime = theTrack->GetGlobalTime();
  G4double energy = theTrack->GetTotalEnergy();
  /////////////////////////////////////////////////////////////////
  G4int detNumber = theTouchable->GetCopyNumber(1);
  G4ThreeVector photonDirection = thePostPoint->GetMomentumDirection();
  G4double photonTheta = photonDirection.theta();
  G4double photonPhi = photonDirection.phi();
  //////////////////////////////////////////////////////////////////////
  // Convert the global coordinate for arriving photons into
  // the local coordinate of the detector
  photonArrive =
      theTouchable->GetHistory()->GetTopTransform().TransformPoint(photonArrive);

  // Creating the hit and add it to the collection
  fPhotonDetHitCollection->insert(
      new PSFPhotonDetHit(photonExit, photonArrive, arrivalTime, energy, detNumber, photonDirection));
  return true;
}

void PSFPhotonDetSD::EndOfEvent(G4HCofThisEvent *)
{
  if (verboseLevel > 0)
  {
    G4int nofHits = fPhotonDetHitCollection->entries();
    G4cout << G4endl
           << "-------->Hits Collection: in this event there are " << std::setw(5) << nofHits
           << " hits in the photon detector: " << G4endl;
    if (verboseLevel > 1)
    {
      for (G4int i = 0; i < nofHits; i++)
        (*fPhotonDetHitCollection)[i]->Print();
    }
  }
}
